package com.jh.cloud.bus.spring;

import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.context.PayloadApplicationEvent;
import org.springframework.context.event.EventListener;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class SpringEventController implements ApplicationEventPublisherAware {

    private ApplicationEventPublisher applicationEventPublisher;

    @Override
    public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
        this.applicationEventPublisher = applicationEventPublisher;
    }

    @GetMapping("/send/event")
    public String sendEvent(@RequestParam String message) {
        applicationEventPublisher.publishEvent(message);
        return "OK";
    }

    @EventListener
    public void onMessage(PayloadApplicationEvent event) {
        System.err.println("接收事件：" + event.getPayload());
    }

}
